package com.advertisementsystem.Controller;

import com.advertisementsystem.Dto.ChatMessageDTO;
import com.advertisementsystem.Exception.ResponseResult;
import com.advertisementsystem.Servie.ChatService;
import com.advertisementsystem.entity.ChatMessages;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/chat")
@Slf4j
public class ChatController {

    @Autowired
    private ChatService chatService;

    @PostMapping("/send")
    public ResponseResult<ChatMessages> sendMessage(@RequestBody @Valid ChatMessageDTO messageDTO) {
        log.info("messageDTO = " + messageDTO);

        return ResponseResult.success(chatService.sendMessage(messageDTO));
    }

    @GetMapping("/history/{negotiationId}")
    public ResponseResult<List<ChatMessages>> getMessageHistory(@PathVariable Long negotiationId) {
        return ResponseResult.success(chatService.getMessageHistory(negotiationId));
    }

    @PutMapping("/messages/{messageId}/read")
    public ResponseResult<Void> markMessageAsRead(@PathVariable String messageId) {
        chatService.markMessageAsRead(messageId);
        return ResponseResult.success();
    }

    @DeleteMapping("/messages/{messageId}")
    public ResponseResult<Void> recallMessage(
            @PathVariable String messageId,
            @RequestParam Long userId) {
        chatService.recallMessage(messageId, userId);
        return ResponseResult.success();
    }
}
